package mainPackage;

import inputPackage.KeyboardManager;
import inputPackage.KeyboardManager3D;

import java.awt.CardLayout;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JPanel;

import modelPackage.Player;
import modelPackage.WorldManager;
import viewPackage.DrawingManager;
import viewPackage.DrawingManager3D;
import viewPackage.KaPow;
import viewPackage.menus.PausePanel;


/*
 * LOST ON CAMPUS
 * Engineering 5895: Software Design
 * Final Project
 * Iteration 1
 * March 20, 2009
 * 
 * Alex Conrad 200529006
 * Nikhil Bhatia 200551901
 * 
 * This is the framework of a 2D platformer.  It lacks in some of the aspects which make a game a game, however, 
 * it contains all of the necessary components to achieve the goal of a fully functional game.
 *  
 * */


@SuppressWarnings("serial")
public class GameMain extends JPanel{


	
	public GameMain(GFrame gameFrame, String PlayerID)  {
		
		/*
		 * At this point the game should be running either in full screen or in windowed mode, based on the choices made.
		 * We should also have access to the resolution of that window.
		 * */

		WorldManager WMan = new WorldManager();
				
		Player newPlayer = new Player(PlayerID);
		newPlayer.setMap(WMan.get3DMap("eng", 12));
		
		WMan.addPlayer(newPlayer);	
		

		DrawingManager DMan = new DrawingManager(WMan, PlayerID, gameFrame.getWidth(), gameFrame.getHeight());
		DrawingManager3D DMan3D = new DrawingManager3D(WMan, PlayerID, gameFrame.getWidth(), gameFrame.getHeight());
		Cursor NULL_CURSOR = Toolkit.getDefaultToolkit().createCustomCursor(Toolkit.getDefaultToolkit().getImage(""), new Point(0,0), "NULL_CURSOR");
		DMan.setCursor(NULL_CURSOR);
		DMan3D.setCursor(NULL_CURSOR);
		
		
		
		KeyboardManager KMan = new KeyboardManager(newPlayer, gameFrame);
		DMan.addKeyListener(KMan);
		
		KeyboardManager3D KMan3D = new KeyboardManager3D(newPlayer, gameFrame);
		DMan3D.addKeyListener(KMan3D);
		
		gameFrame.getKeyResource().writeToFile();
		
		CardLayout cl = new CardLayout();
		
		gameFrame.getContentPane().removeAll();
		
		gameFrame.getContentPane().setLayout(cl);
		
		KaPow kapow = gameFrame.getKaPow();
		
		gameFrame.getContentPane().add(DMan, "Game");
		gameFrame.getContentPane().add(DMan3D, "3D");
		gameFrame.getContentPane().add(new PausePanel(gameFrame, newPlayer), "Pause");
		gameFrame.getContentPane().add(kapow, "kapow");

		cl.show(gameFrame.getContentPane(), "Game");
		DMan.grabFocus();
		
		//Then here we have the main game loop.
		Timer timer = new Timer();
		int PERIOD = 17;
		timer.schedule(new UpdateTask(DMan, DMan3D, WMan), 0, PERIOD);	
		
		//Starts the MP3 player
		//MP3Player mp3Player = new MP3Player("song/Robots.mp3");
		//mp3Player.play();
		//mp3Player.stop();
	}
	
}

class UpdateTask extends TimerTask {
	
	private DrawingManager DMan;
	private DrawingManager3D DMan3D;
	private WorldManager WMan;
	private long previousTime;
	private boolean gamePaused;
	
		
	public UpdateTask(DrawingManager DM, DrawingManager3D DM3D, WorldManager WM) {
		DMan = DM;
		DMan3D = DM3D;
		WMan = WM;
		previousTime = System.currentTimeMillis();
		gamePaused = false;	
	}
	
	@Override
	public void run() {
		long currentTime = System.currentTimeMillis();
		long elapsedTime = currentTime - previousTime;
		previousTime = currentTime;
		
		gamePaused = false;
		if (WMan.getPlayers().size() != 0) {
			for (int i = 0; i < WMan.getPlayers().size(); i++) {
				if (WMan.getPlayers().get(i).getPaused() == true) {
					gamePaused = true;
				}
			}
		}
		
		if (gamePaused != true) {
			WMan.updateSprites(elapsedTime);
			DMan.repaint();
			DMan3D.repaint();			
		}
	}			
}